﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sharp.DataTransferObject;
using Sharp.Domain.PlatformBoundedContext.PermissionModule.Aggregates;
using Sharp.Infrastructure;

namespace Sharp.Application.Platform
{
    public partial class UserServiceImpl
    {
        /// <summary>
        ///     全部权限
        /// </summary>
        /// <returns></returns>
        public IEnumerable<PermissionDTO> AllPermissions()
        {
            return _permissionRepository.GetList().To<Permission, PermissionDTO>();
        }

        /// <summary>
        ///     保存权限
        /// </summary>
        /// <param name="ids">被勾选的权限列表</param>
        /// <param name="owner">所有者</param>
        /// <param name="ownerId">所有者id</param>
        /// <remarks>通过所有者来判断具体是给用户或者用户组进行权限配置</remarks>
        /// <returns></returns>
        public Result SavePermission(IList<Guid> ids, string owner, Guid ownerId)
        {
            return _domainService.SavePermission(ids, owner, ownerId);
        }


        /// <summary>
        ///     获取用户权限
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public IEnumerable<PermissionDTO> GetPermissionById(Guid uid)
        {
            if (uid.IsEmpty())
            {
                throw new NullReferenceException("参数id不能为空。");
            }
            return _domainService.GetPermissionById(uid).To<Permission, PermissionDTO>();
        }
        /// <summary>
        /// 更新权限库
        /// </summary>
        /// <param name="dtos"></param>
        /// <returns></returns>
        public Result UpdatePermission(IList<PermissionDTO> dtos)
        {
            var allList = _permissionRepository.GetList().OrderByDescending(x => x.Route);
            dtos.ToList().ForEach(x =>
            {
                Guid id = Guid.Parse(x.Code.IsNullOrEmpty() ? x.Route.Md5() : x.Code.Md5());
                if (!_permissionRepository.Exists(y => y.Id == id))
                {
                    x.Id = id;

                    var parents = allList.Where(c => c.Category == "01" && x.Route.StartsWith(c.Route) && c.Route.Length > 0);
                    if (parents.Count() == 1)
                    {
                        x.ParentId = parents.First().Id;
                    }
                    else
                    {
                        x.ParentId = parents.Last().Id;
                        //#if DEBUG
                        //                        throw new Exception("可能有bug。");
                        //#endif
                    }

                    _permissionRepository.Add(x.To<Permission>());
                }
                else
                {
                    var p = _permissionRepository.Get(id);
                    p.Code = x.Code;
                    p.Route = x.Route;
                    p.Title = x.Title;
                    _permissionRepository.Update(p);
                }
            });

            //TODO:删除垃圾权限数据


            _permissionRepository.Context.Commit();
            return new Result();
        }
    }
}
